
angular.module('QualifyInspection')

.factory('QualifyInspectionMaster', ['$q', 'Http', 'QUALIFYINSPECTIONCONSTANTS', 'User', 'Device', 
    function($q, Http, QUALIFYINSPECTIONCONSTANTS, User, Device) {
        var o = {};

        o.scanCode = function (barcode) {
            var device = Device.get(),
                data = {
                    Barcode: barcode,
                    UserId: User.get('_id')
                };
            if (device.Work) {
                data.WorkId = device.Work._id;
                data.WorkName = device.Work.Name;
            }
            if (device.WorkLocation) {
                data.WorkLocationId = device.WorkLocation._id;
                data.WorkLocationName = device.WorkLocation.Name;
            }
            return Http.post({
                url: QUALIFYINSPECTIONCONSTANTS.scanCodeURL,
                data: data
            });
        };

        o.submit = function (barcodes) {
            var device = Device.get(),
                data = {
                    Barcodes: barcodes,
                    UserId: User.get('_id')
                };
            if (device.Terminal) {
                data.TerminalId = device.Terminal._id;
                data.TerminalName = device.Terminal.Name;
            }
            if (device.Work) {
                data.WorkId = device.Work._id;
                data.WorkName = device.Work.Name;
            }
            if (device.WorkLocation) {
                data.WorkLocationId = device.WorkLocation._id;
                data.WorkLocationName = device.WorkLocation.Name;
            }
            if (device.Department) {
                data.Department = device.Department._id;
            }
            return Http.post({
                url: QUALIFYINSPECTIONCONSTANTS.submitURL,
                data: data
            });
        };

        return o;
    }
]);
